import apiRequest from '@/utils/api-request';

export interface DataItem {
  id: string;
  name: string;
  value: any;
  timestamp?: number;
}

export class ApiService {
  async getData(params?: Record<string, any>) {
    const query = params ? `?${new URLSearchParams(params).toString()}` : '';
    return apiRequest.get<DataItem[]>(`/api/data${query}`);
  }

  async postData(data: Omit<DataItem, 'id'>) {
    return apiRequest.post<DataItem>('/api/data', data);
  }

  async updateData(id: string, data: Partial<DataItem>) {
    return apiRequest.put<DataItem>(`/api/data/${id}`, data);
  }

  async deleteData(id: string) {
    return apiRequest.delete(`/api/data/${id}`);
  }

  async getStatus() {
    return apiRequest.get<{ status: string; timestamp: number }>('/api/status');
  }

  async uploadImage(file: File) {
    const formData = new FormData();
    formData.append('file', file);

    return apiRequest.post<{ url: string; filename: string }>('/upload', formData, {
      headers: {
        // 不设置Content-Type，让浏览器自动设置multipart/form-data
      }
    });
  }

  async deleteImage(filename: string) {
    return apiRequest.delete<{ message: string }>(`/image/delete/${filename}`);
  }
}

export const apiService = new ApiService();
